package sub

import (
	"fmt"
	"quipus/cmd/qpc/sub"
	v1pb "quipus/proto/gen/api/v1"

	"github.com/spf13/cobra"
)

var (
	tenantName string
	ShowName   string
)

func init() {
	createCmd.PersistentFlags().StringVarP(&tenantName, "name", "n", "", "tenant name")
	createCmd.PersistentFlags().StringVarP(&ShowName, "show", "s", "", "show tenant name")

	tenantCmd.AddCommand(createCmd)
}

var createCmd = &cobra.Command{
	Use:   "create",
	Short: "Run the tenant to manager tenants",
	RunE: func(cmd *cobra.Command, args []string) error {
		qconn, e := sub.NewGrpcConnect(sub.QpConfig.QPAddress)
		if e != nil {
			return e
		}

		qclient := v1pb.NewTenantServiceClient(qconn)
		ctxWithToken := sub.NewCtxWithToken(sub.QpConfig.Token)
		in := &v1pb.CreateTenantRequest{TenantName: tenantName, ShowName: ShowName}
		rsp, err := qclient.CreateTenant(ctxWithToken, in)
		if err != nil {
			return err
		}
		fmt.Print(rsp)
		return nil
	},
}
